package config

import (
	_ "embed"
	"fmt"
	"gopkg.in/yaml.v3"
)

var Config YamlConfig

type YamlConfig struct {
	App    App          `yaml:"app"`
	Server ServerConfig `yaml:"server"`
	Log    LogConfig    `yaml:"log"`
}

type App struct {
	Name         string `yaml:"name"`
	Version      string `yaml:"version"`
	Introduction string `yaml:"introduction"`
}

type ServerConfig struct {
	Host    string `yaml:"host"`
	Port    int    `yaml:"port"`
	AppMode string `yaml:"appMode"`
}

//go:embed config-dev.yaml
var configData []byte

// InitConfigDev 初始化yaml config
func init() {
	//file, err := os.ReadFile("./config/config-dev.yaml")
	//if err != nil {
	//	fmt.Println("读取配置 config-dev.yaml 失败", err)
	//	panic(err)
	//}

	err := yaml.Unmarshal(configData, &Config)
	if err != nil {
		fmt.Println("yaml unmarshal fail")
		panic(err)
	}
}

// PrintConfig 打印配置
func PrintConfig() {
	fmt.Println("项目配置信息")
	fmt.Println("*                        ")
	fmt.Println("*   Welcome to code-go   ")
	fmt.Printf("*   Introduction: %s\n", Config.App.Introduction)
	fmt.Printf("*   Environment: %s\n", Config.Server.AppMode)
	fmt.Printf("*   Click url：http://localhost:%d\n", Config.Server.Port)
	fmt.Println("*                        ")
	fmt.Println("*********************项目配置信息*****************************")
}

type LogConfig struct {
	ChanSize   int `yaml:"chan-size"`
	SaveLogNum int `yaml:"save-log-num"`
}
